<?php


namespace app\crm\model;

use think\facade\Db;
use app\admin\model\Common;

class CustomerGrade extends Common
{

    /**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如CRM模块用crm作为数据表前缀
     */
     protected $name = 'customer_grade';

    /**
     * 列表
     * @param  array $request 请求参数
     * @return array
     * @author Mr.Huang
     * @create 2021-06-23 11:48
     */
    public function getDataList(array $request): array
    {
        try {
            $list              = $this->order("id asc")->select()->toArray();
            $data              = [];
            $data['list']      = $list ?? [];
            $data['dataCount'] = $dataCount ?? 0;
            return $data;
        } catch (\Exception $e) {
        }

        $this->error = '暂无此数据';
        return [];
    }
    public function gradeDetail(array $request): array
    {
        try {
            $info        = $this->order("id desc")->find()->toArray();
            $returnData  = array();
            if ($info) {
                $minimum  = $info['maximum'] ? $info['maximum'] : $info['minimum'];
            }else{
                $minimum  = 0;
            }
            $where    = [];
            $where[]  = ['id','=',$request['id']];
            $find              = $this->where($where)->find()->toArray();
            if ($find) {
                $data                = $find;
                $data['set_minimum'] = $minimum;
            }else{
                $data     = [];
            }
            return $data;
        } catch (\Exception $e) {
        }

        $this->error = '暂无此数据';
        return [];
    }
    /**
     * 添加会员等级的基础数据获取
     * @param  array $request 请求参数
     * @return array
     * @author Mr.Huang
     * @create 2021-06-23 11:48
     */
    public function addGradeDetail(array $request): array
    {
        try {
            $info        = $this->order("id desc")->find()->toArray();
            $returnData  = array();
            if ($info) {
                $minimum  = $info['maximum'] ? $info['maximum'] : $info['minimum'];
            }else{
                $minimum  = 0;
            }
            $data          = [];
            $data['minimum']  = $minimum;
            return $data;
        } catch (\Exception $e) {
        }

        $this->error = '请求失败';
        return [];
    }

   /**
    * 添加会员等级
    **/
   public function addGrade(array $request): array
   {
    try {
            $info         = $this->order("id desc")->find()->toArray();
            $returnData   = array();
            if ($info) {
                $minimum  = $info['maximum'];
                if (intval($minimum) == 0) {
                    $this->error = '请先设置已存在最后一级的最大金额';
                    return [];
                }
            }else{
                $minimum  = 0;
            }
            $validate = new \app\crm\validate\CustomerGrade();
            if (!$validate->scene('create')->check($request)) {
                $this->error = $validate->getError();
                return [];
            }
            if ($minimum > $request['minimum']) {
                $this->error = '当前等级金额设置错误，当前最小值必须大于等于'.$minimum;
                return [];
            }
            if ($request['maximum'] <= $request['minimum']) {
                $this->error = '当前等级金额最大值小于最小值';
                return [];
            }
            $addData                = [];
            $addData['name']        = $request['name'];
            $addData['minimum']     = $request['minimum'];
            $addData['maximum']     = $request['maximum'];
            $addData['remarks']     = $request['remarks'] ?? '';
            $addData['create_time'] = time();
            $addData['update_time'] = time();
            $id = $this->insertGetId($addData);

            // 设置客户的等级 -开始
            $where   = [];
            // $where[] = ['consumption','>=',$request['minimum']];
            // $where[] = ['consumption','<',$request['maximum']];
            // Db::name['customer']->where($where)->update(['membership_leve'=>$id]);
            // 设置客户的等级 -结束

            $data['id']             = $id;
            return $data;
        } catch (\Exception $e) {
        }

        $this->error = '添加失败';
        return [];
   }
    /**
    * 编辑会员等级
    **/
   public function saveGrade(array $request): array
   {
    try {
            $id = $request['id'] ?? 0;
            if ($id == 0) {
                $this->error = '请选择修改等级id';
                return [];
            }
            // $info         = $this->order("id desc")->find()->toArray();
            // $returnData   = array();
            // if ($info) {
            //     $minimum  = $info['maximum'];
            //     if (intval($minimum) == 0) {
            //         $this->error = '请先设置已存在最后一级的最大金额';
            //         return [];
            //     }
            // }else{
            //     $minimum  = 0;
            // }
            // $validate = new \app\crm\validate\CustomerGrade();
            // if (!$validate->scene('create')->check($request)) {
            //     $this->error = $validate->getError();
            //     return [];
            // }
            // if ($minimum > $request['minimum']) {
            //     $this->error = '当前等级金额设置错误，当前最小值必须大于等于'.$minimum;
            //     return [];
            // }
            // if ($request['maximum'] <= $request['minimum']) {
            //     $this->error = '当前等级金额最大值小于最小值';
            //     return [];
            // }
            // print_r($request);exit;
            $saveData                = [];
            $saveData['name']        = $request['name'];
            $saveData['minimum']     = $request['minimum'];
            $saveData['maximum']     = $request['maximum'];
            $saveData['remarks']     = $request['remarks'] ?? '';
            $saveData['update_time'] = time();
            $where = [['id','=',$id]];
            $id = $this->where($where)->update($saveData);

            // 设置客户的等级 -开始
            // $where   = [];
            // $where[] = ['consumption','>=',$request['minimum']];
            // $where[] = ['consumption','<',$request['maximum']];
            // Db::name['customer']->where($where)->update(['membership_leve'=>$id]);
            // 设置客户的等级 -结束

            $data['id']             = $id;
            return $data;
        } catch (\Exception $e) {
        }

        $this->error = '编辑失败';
        return [];
   }
}
